package sd.czw.com.mmgapp.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import sd.czw.com.mmgapp.Contants;


/**
 * Created by czw
 * desc 描述：
 * on 2017/5/18.
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, Contants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
           // LogUtils.d("微信支付返回的code" + resp.errCode);
            switch (resp.errCode) {
                case 0://成功
                    Toast.makeText(WXPayEntryActivity.this, ""+"支付成功", Toast.LENGTH_SHORT).show();
//                    SpUtils spUtils = new SpUtils(this,"orderno");
//                    final String orderno = spUtils.getString("orderno");
//                    SpUtils spUtils1 = new SpUtils(this,"token");
//                    String token = spUtils1.getString("token");
//                    OkHttpUtils.post().url(Contants.PLACE_ISPAY).addParams("time",Contants.TIME).addParams("sign",Contants.SIGN).addParams("token",token).addParams("orderno",orderno)
//                            .build().execute(new RegistRequestCallback() {
//                        @Override
//                        public void onError(Call call, Exception e, int id) {
//
//                        }
//
//                        @Override
//                        public void onResponse(RegistModel response, int id) {
//                            int errcode = response.getErrcode();
//                            if (errcode == 0){
//                                //startActivity(new Intent(WXPayEntryActivity.this, PlaceDetailActivity.class));
//                                //Toast.makeText(WXPayEntryActivity.this, "支付成功----暂时不知道跳什么页面，只有finish当前页了", Toast.LENGTH_SHORT).show();
//                                Intent intent = new Intent(WXPayEntryActivity.this, UserOrderDetailActivity.class);
//                                intent.putExtra("orderno",orderno);
//                                startActivity(intent);
//                               // finish();
//                            }
//                        }
//                    });
                    break;
                case -1://取消
                   // LogUtils.d("签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、您的微信账号异常等。");
                    Toast.makeText(getApplicationContext(), "支付失败", Toast.LENGTH_LONG).show();
                    finish();
                  //  startActivity(new Intent(WXPayEntryActivity.this, PlaceOrderPayActivity.class));

                    break;
                case -2://被拒绝
                   // LogUtils.d("用户取消支付");
                    onBackPressed();
                    break;
                default://返回
                   // LogUtils.d("用户返回");
                    finish();
                    break;
            }

        }
    }


    public void finishPay() {


    }
}
